package com.user.config;



import jakarta.servlet.*;

import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 请求拦截，避免服务绕过接口被直接访问
 */
@Component
@WebFilter(filterName = "BaseFilter",urlPatterns = {"/user/**"})
public class BaseFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("init filter");
    }
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException, IOException {
        System.out.println("进入过滤器========");
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String gateway = request.getHeader("gatewayKey");
        System.out.println("gateway = " + gateway);
        if(gateway == null || gateway.equals("") || !gateway.equals("key")){
            return;
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }
 
    @Override
    public void destroy() {
        System.out.println("destroy filter");
    }
 
}